//
//  SearchFindViewController.m
//  Find
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "SearchFindViewController.h"

@interface SearchFindViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dataArray;
@end

@implementation SearchFindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(actionSearch:) name:@"SEARCHFIND" object:nil];
    [self addTableView];
}

- (void)actionSearch:(NSNotification *)notification
{
    self.dataArray = [NSMutableArray array];
    self.dataArray = [notification.object valueForKey:@"array"];
    [self.tableView reloadData];
}

- (void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
}

#pragma mark === tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

// 每个分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"FindListCell";
    LostListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[LostListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    FindMessageModel *findModel = self.dataArray[indexPath.row];
    
    AVUser *user1 = findModel.user;
    //    AVUser *user2 = [AVQuery getUserObjectWithId:user1.objectId];  // 同步请求数据导致界面卡顿
    AVQuery * query = [AVQuery queryWithClassName:@"_User"];
    [query  getObjectInBackgroundWithId:user1.objectId block:^(AVObject *object, NSError *error) {
        NSString *urlString = [[object valueForKey:@"photoImage"] getThumbnailURLWithScaleToFit:NO width:100 height:100];
        [cell.listImage sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }];
    
    // 用户名
    cell.listTextLabel.text = findModel.context;
    // 上传的日期
    NSDateFormatter *dateFoematter = [[NSDateFormatter alloc] init];
    [dateFoematter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSRange range = NSMakeRange(5, 11);
    cell.listTimeLabel.text = [[dateFoematter stringFromDate: findModel.createdAt] substringWithRange:range];
    // 距离
    cell.listAddressLabel.text = findModel.location;
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *objectID = [self.dataArray[indexPath.row] objectId];
 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"FIND" object:@{@"objectID":objectID,@"flag":@"find"}];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
